package xyz.shenmegui.foodcheckserver.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import xyz.shenmegui.foodcheckserver.DO.Collection;
import xyz.shenmegui.foodcheckserver.DO.Compound;
import xyz.shenmegui.foodcheckserver.DO.Record;
import xyz.shenmegui.foodcheckserver.DO.User;
import xyz.shenmegui.foodcheckserver.VO.*;
import xyz.shenmegui.foodcheckserver.annotation.Login;
import xyz.shenmegui.foodcheckserver.annotation.LoginUser;
import xyz.shenmegui.foodcheckserver.service.CollectionService;
import xyz.shenmegui.foodcheckserver.service.RecordService;
import xyz.shenmegui.foodcheckserver.service.UserService;
import xyz.shenmegui.foodcheckserver.http.HttpResponse;
import xyz.shenmegui.foodcheckserver.utils.JWTUtil;
import xyz.shenmegui.foodcheckserver.utils.ObjectUtil;
import xyz.shenmegui.foodcheckserver.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private CollectionService collectionService;

    @PostMapping("/wxLogin")
    public HttpResponse wxLogin(@RequestBody WxLoginForm form) {
        // 根据前端传入数据，从库中查询用户
        Integer userId = userService.wxLogin(form);
        // 根据用户id，生成token
        String token = jwtUtil.generateToken(userId);
        // 将 token 放入返回结果中
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtil.getExpire());
        // 返回数据
        return HttpResponse.ok(map);
    }

    @Login
    @RequestMapping("/userInfo")
    public HttpResponse getUserInfo(@LoginUser User user) {
//        System.out.println("UserController--getUserInfo--user:" + user);
        return HttpResponse.ok().put("userinfo", user);
    }

    @Login
    @GetMapping("/updateUsername")
    public HttpResponse updateUsername(@LoginUser User user, @RequestParam String username) {
        user.setUsername(username);
        userService.updateUserInfo(user);
        return HttpResponse.ok().put("username", username);
    }

    /**
     * 获取所有搜索记录
     * @param user
     * @return
     */
    @Login
    @PostMapping("/getRecords")
    public HttpResponse getRecords(@LoginUser User user) {
        List<RecordDetail> records = recordService.selectByUserId(user.getId());
        return HttpResponse.ok().put("data", records);
    }


    /**
     *
     * @param user  登录用户的信息
     * @param userUpdateInfo    前端传入的需要更新的用户信息
     * @return
     */
    @Login
    @PostMapping("/updateUserInfo")
    public HttpResponse updateUserInfo(@LoginUser User user, @RequestBody User userUpdateInfo) {
        // 设置对应属性
        user.setAge(userUpdateInfo.getAge());
        user.setSex(userUpdateInfo.getSex());
        user.setHeight(userUpdateInfo.getHeight());
        user.setWeight(userUpdateInfo.getWeight());
        user.setWeight(userUpdateInfo.getWeight());
        user.setTarget(userUpdateInfo.getTarget());
        user.setDiseases(userUpdateInfo.getDiseases());
        user.setTaboos(userUpdateInfo.getTaboos());
        // 计算 BMI 判断体型
        if (user.getHeight() != null && user.getHeight() > 0 && user.getWeight() != null && user.getWeight() > 0) {
            Double bmi = user.getWeight() / (user.getHeight() * user.getHeight());
            String shape = StringUtils.EMPTY;
            if (bmi < 18.5D) {
                shape = "过轻";
            } else if (bmi >= 18.5D && bmi < 24.9D) {
                shape = "正常";
            } else if (bmi >= 24.9D && bmi < 28D) {
                shape = "过重";
            } else {
                shape = "肥胖";
            }
            user.setShape(shape);
        }
        // 更新到数据库中
        int rows = userService.updateUserInfo(user);
        return HttpResponse.ok();
    }

    @Login
    @PostMapping("/getCollections")
    public HttpResponse getCollections(@LoginUser User user) {
        // 从数据库中查询对应用户的收藏记录
        CollectionList collectionList = collectionService.getCollectionByUserId(user.getId());
        // 返回结果
        return HttpResponse.okWithData(collectionList);
    }


    /**
     * 新增收藏
     * @param user
     * @param collectionVo
     * @return
     */
    @Login
    @PostMapping("/addCollection")
    public HttpResponse addCollection(@LoginUser User user, @RequestBody CollectionVo collectionVo) {
        // 对象声明与初始化
        Collection collection = new Collection();
        collection.setUserId(user.getId());
        if ("1".equals(collectionVo.getType())) {
            // 早餐
            collection.setMenuId(collectionVo.getId());
        } else if ("2".equals(collectionVo.getType())) {
            // 午餐
            collection.setMenuId(collectionVo.getId());
        } else if ("3".equals(collectionVo.getType())) {
            // 晚餐
            collection.setMenuId(collectionVo.getId());
        } else if ("food".equals(collectionVo.getType())) {
            // 食物
            collection.setFoodId(collectionVo.getId());
        }
        // 加入到数据库中
        int rows = collectionService.addCollection(collection);
        return HttpResponse.ok();
    }


    /**
     * 删除收藏
     * @param user
     * @param collection
     * @return
     */
    @Login
    @PostMapping("/deleteCollection")
    public HttpResponse deleteCollection(@LoginUser User user, @RequestBody Collection collection) {
        // 在数据库中删除
        int rows = collectionService.deleteCollection(collection);
        return HttpResponse.ok();
    }
}
